package com.mytest.offer;

import lombok.extern.slf4j.Slf4j;

import java.util.HashSet;

@Slf4j
public class SolutionJZ22 {

    public static void main(String[] args) {
        SolutionJZ22 solutionJZ6 = new SolutionJZ22();
        ListNode listNode = new ListNode(1);
        ListNode listNode1 = new ListNode(2);
        ListNode listNode2 = new ListNode(3);
        ListNode listNode3 = new ListNode(4);
        ListNode listNode4 = new ListNode(5);

        listNode.next = listNode1;
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;


        solutionJZ6.findKthToTail(listNode,2);
    }

    public ListNode findKthToTail (ListNode pHead, int k) {
        // write code here
        ListNode fast = pHead;
        ListNode slow = pHead;
        for(int i = 0;i< k;i++){
            if (fast != null){
                fast = fast.next;
            }else{
                return slow = null;
            }
        }
        while(fast != null){
            fast = fast.next;
            slow = slow.next;

        }

        return slow;
    }
}
